//
// Created on 2025/11/12.
// Author 秦光磊.

#ifndef MYAPPLICATION_EGL_CORE_H
#define MYAPPLICATION_EGL_CORE_H
#include <EGL/egl.h>
#include <EGL/eglext.h>
#include <EGL/eglplatform.h>
#include <GLES3/gl3.h>
#include <string>
class EGLCore {
public:
    explicit EGLCore(){};
    ~EGLCore(){};
    bool EglContextInit(void *window);
    bool EglContextInit2(void *window, int width, int height);
    bool CreateEnvironment();
    bool BuildRenderEntrance(void *nativeWindow, EGLDisplay *eglDisplay, EGLSurface *eglSurface);
    bool BuildShaderProgram(GLuint *program, int width, int height);
    bool BuildViewport(GLuint *program, float *drawWidth, float *drawHeight, float *screenRatio, int width, int height,
                       GLfloat backgroundColor[4], int backgroundSize);
    void Draw(int &hasDraw);
    void Background();
    void ChangeColor(int &hasChangeColor);
    void Release();
    void UpdateSize(int width, int height);
    bool DrawAnyObject(std::vector<GLfloat> &shapeVertices, int vertSize, GLfloat drawColor[4], int colorSize,
                       int *geometryAttribute, int attrSize);

private:
    GLuint LoaderShader(GLenum type, const char *shaderSrc);
    GLuint CreateProgram(const char *vertexShader, const char *fragShader);
    GLuint PrepareDraw();
    bool ExecuteDraw(GLint position, const GLfloat *color, const GLfloat shaperVertices[], unsigned long vertSize);
    bool ExecuteDrawStar(GLint position, const GLfloat *color, const GLfloat shaperVertices[], unsigned long vertSize);
    bool ExecuteDrawNewStar(GLint position, const GLfloat *color, const GLfloat shaperVertices[],
                            unsigned long vertSize);
    void Rotate2d(GLfloat centerX, GLfloat centerY, GLfloat *rotateX, GLfloat *rotateY, GLfloat theta);
    bool FinishDraw();

private:
    EGLNativeWindowType eglWindow_;
    EGLDisplay eglDisplay_ = EGL_NO_DISPLAY;
    EGLConfig eglConfig_ = EGL_NO_CONFIG_KHR;
    EGLSurface eglSurface_ = EGL_NO_SURFACE;
    EGLContext eglContext_ = EGL_NO_CONTEXT;
    GLuint program_;
    bool flag_ = false;
    int width_;
    int height_;
    GLfloat widthPercent_;
};

#endif // MYAPPLICATION_EGL_CORE_H
